# -*- coding: utf-8 -*-
# @Time    : 2022/6/28 11:37
# @Author  : AUGUS
# @Email   : augus@jilin.com
# @File    : serializers.py
# @Software: PyCharm
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from users.serializers import UserSerializer
from .models import *


class ClassesSerializer(ModelSerializer):
    memberDetail = SerializerMethodField()

    class Meta:
        model = classes
        exclude = ['is_delete']

    def get_memberDetail(self, instance):
        members = UserSerializer(instance.member.all(), many=True).data  # 获取当前班级所有成员并序列化  # 得到班级的所有学生
        return {
            'teachers': [i['username'] for i in members if i['is_superuser'] or 1 in i['groups']],
            'students': [i['username'] for i in members if 2 in i['groups']]
        }


class CreateUserSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'first_name', 'email']
        extra_kwargs = {
            'username': {'required': True},
            'email': {'required': True},
        }